package com.centaline.androidsalesblog.base;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.IndexFragmentAdapter;
import com.centaline.androidsalesblog.adapter.NullAdapter;
import com.centaline.androidsalesblog.application.MyLocation;
import com.centaline.androidsalesblog.constants.DefaultHint;
import com.centaline.androidsalesblog.rx.map.RxMap;
import com.centaline.androidsalesblog.widgets.IndexViewPager;
import com.centaline.androidsalesblog.widgets.ListSearchView;
import com.centaline.androidsalesblog.widgets.dropdown.DropDownView;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;

import rx.functions.Action1;

/**
 * 列表\地图双模式基类
 * Created by guilin on 16/2/18.
 */
public abstract class BaseListMapActivity extends BaseActivity implements View.OnClickListener {

    protected DropDownView dropDownView;
    protected IndexViewPager viewPager;
    protected FloatingActionButton fab;
    protected ListSearchView listSearchView;

    protected IndexFragmentAdapter indexFragmentAdapter;
    protected ArrayList<BaseFragment> fragmentArrayList = new ArrayList<>();
    protected LocationClient locationClient;
    protected boolean showFab;//是否显示足迹
    protected boolean needLocation;//是否需要定位
    protected int clearType;//0:区域板块;1:关键字
    protected CharSequence searchContent;//搜索关键字

    @Override
    protected int setLayoutId() {
        return R.layout.activity_list_map;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
        dropDownView = (DropDownView) findViewById(R.id.dropDownView);
        viewPager = (IndexViewPager) findViewById(R.id.viewPager);
        fab = (FloatingActionButton) findViewById(R.id.fab);
    }

    @Override
    protected void initViews() {
        fab.setOnClickListener(this);
        showFab = setShowFab();
        fab.setVisibility(showFab ? View.VISIBLE : View.GONE);

        initDrop();
        fragmentArrayList.add(getFragment1());
        fragmentArrayList.add(getFragment2());
        indexFragmentAdapter = new IndexFragmentAdapter(getSupportFragmentManager(), fragmentArrayList);
//        viewPager.setPageTransformer(true, new CubeFlipTransformer());
        viewPager.setAdapter(indexFragmentAdapter);

        locationClient = MyLocation.getLocationClient();
        RxMap.location(locationClient, false)
                .compose(this.<BDLocation>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<BDLocation>() {
                    @Override
                    public void call(BDLocation bdLocation) {
                        locationInfo(bdLocation);
                    }
                });
    }

    /**
     * 是否显示Fab
     */
    protected boolean setShowFab() {
        return true;
    }

    /**
     * 返回adapter
     */
    protected RecyclerView.Adapter getAdapter() {
        return new NullAdapter();
    }

    /**
     * 初始化下拉菜单
     */
    protected abstract void initDrop();

    /**
     * 第一个BaseFragment
     */
    protected abstract BaseFragment getFragment1();

    /**
     * 第二个BaseFragment
     */
    protected abstract BaseFragment getFragment2();


    /**
     * 下拉刷新动画
     *
     * @param needLocation 需要定位信息
     */
    protected void downRefresh(boolean needLocation) {
        downRefresh(needLocation, false);
    }

    protected void downRefresh(boolean needLocation, boolean clearLocation) {
    }

    /**
     * 定位信息
     */
    protected void locationInfo(BDLocation bdLocation) {
    }

    /**
     * 开始定位
     */
    protected void startLocation() {
        locationClient.start();
    }

    /**
     * 设置类型
     */
    protected DefaultHint getDefaultHint() {
        return DefaultHint.SALE;
    }

    /**
     * 设置回调
     */
    protected ListSearchView.SearchCallback getSearchCallback() {
        return new ListSearchView.SearchCallback() {

            @Override
            public void clear() {

            }

            @Override
            public void gotoSearch() {

            }

            @Override
            public void gotoSearchVoice() {

            }
        };
    }

    /**
     * 设置模式回调
     */
    protected ListSearchView.ModeCallback getModeCallback() {
        return new ListSearchView.ModeCallback() {
            @Override
            public void mode(int mode) {
                viewPager.setCurrentItem(mode, true);
                baiduEvent("C1_4_Click");
                if (showFab) {
                    fab.setVisibility(mode == 0 ? View.VISIBLE : View.GONE);
                }
            }
        };
    }

    /**
     * 设置搜索是否可用
     */
    protected CharSequence getSearchDefaultTitle() {
        return null;
    }

    /**
     * 设置搜索内容
     */
    protected void setSearchContent(CharSequence charSequence) {
        searchContent = charSequence;
        if (listSearchView != null) {
            listSearchView.setSearchContent(charSequence);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_list_map, menu);
        listSearchView = (ListSearchView) menu.findItem(R.id.action_search).getActionView();
        listSearchView.setDefaultHint(getDefaultHint());
        listSearchView.setSearchCallback(getSearchCallback());
        listSearchView.setModeCallback(getModeCallback());
        listSearchView.setSearchDefaultTitle(getSearchDefaultTitle());
        if (!TextUtils.isEmpty(searchContent)) {
            listSearchView.setSearchContent(searchContent);
        }
        return super.onCreateOptionsMenu(menu);
    }
}
